home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / gravity.src < prev    next >
Text File  |  1991-10-19  |  13KB  |  361 lines

  1. %%HP: T(3)A(R)F(.);
  2. @ GRAVITY GAME by David F. Kurth  06Jun91  Ver 1.0
  3. @
  4. @ Original Concept Stolen From Some Ancient HP Desktop Calculator
  5. @
  6. @ Object:  Launch a missile from your station (a cross or X
  7. @ on the left side of the screen) to hit a target (a bull's-eye
  8. @ on the right side of the screen).  Two to five planets affect
  9. @ the missile's path due to their gravitational field.  Larger 
  10. @ diameter planets have more influence.
  11. @
  12. @ Controls: Your only input is the firing angle of your missile.
  13. @ An angle of 0 degrees shoots horizontal to the right.  90 degrees
  14. @ is straight up.  If there is too much planet mass behind your
  15. @ station, you may not be able to reach the target.  On the other
  16. @ hand, an indirect missile path may have surprising results.
  17. @ Be careful, or you may hit yourself!  Your station and the target
  18. @ have no gravity, so they will not "attract" the missile.
  19. @
  20. @ Caution: This game is realtime.  One must realize that when using
  21. @ convention missiles without warp drive travelling way below the 
  22. @ speed of light, it takes time to cover such astronomical distances.
  23. @ This game is ideal for long boring meetings where you have time 
  24. @ to burn and want everyone to think you're still solving those 
  25. @ complicated problems that *required* the purchase of your calculator 
  26. @ in the first place.
  27. @
  28. @ Starting: Download the program to the calculator.  This is in
  29. @ straight ASCII format, so there is no need to mess with ASC->
  30. @ binary, uuencode, or any other format.  Then push the GRAV menu key 
  31. @ to start the program.
  32. @
  33. @ For a simple demo, enter "2" for a seed and 10 degrees for the angle.
  34. @ This direct shot takes about 30 seconds.
  35. @ For a crazy demo, enter "2" for a seed and 20 degrees for the angle.
  36. @ Now wait about 7 minutes and see what a different path the missile
  37. @ follows to hit the target.  When the missile is travelling, any
  38. @ keystroke (other than ATTN/ON) will abort the current shot.  You 
  39. @ may abort because you think your shot will miss.  If you let the 
  40. @ missile continue though, you may find the missile will go around 
  41. @ again and eventually hit the target.  This is what makes the game
  42. @ fun to watch and predict.
  43. @
  44. @ This is presented with no rights reserved.  I take no responsibility
  45. @ for any damage or injury to anyone caused by this program.  I
  46. @ believe the physics are correct, but then again all the constants
  47. @ are made up to fit the "universe" on the screen.
  48. @ I'm no expert on the HP48 (I've only had one since Feb).  I only
  49. @ wish things would go faster.  If you find a way to speed things up, 
  50. @ please post the info so I and everyone else may be enlightened.  
  51. @ Above all, have fun and maybe you'll learn something new.
  52. @
  53. @ Directory
  54. @ Checksum # CDEEh
  55. @ Bytes      4654
  56. @ Checksum will change after program is run due to creation of variables
  57. @
  58. @
  59. DIR
  60.   GRAV
  61.     \<< RCLF \-> f           @ Save status
  62.       \<< 2 CF INIT 2 SF     @ Set up variables
  63.         WHILE 2 FS?          @ Start main loop
  64.         REPEAT
  65.           SENDMIS
  66.         END
  67.         f STOF               @ Restore status
  68.       \>>
  69.       { GRAV } ORDER 2 MENU  @ Restore VAR menu with GRAV first
  70.       KILL                   @ Remove HALT annuciator.
  71.     \>>
  72.   INIT
  73.     \<< GETSEED ERASE { #0d #0d} PVIEW
  74.       5 'MAX.PL' STO       @ Max Number of Planets
  75.       4 'T.SIZE' STO       @ Target Size radius
  76.       3 'S.SIZE' STO       @ Station Size radius
  77.       10 'MAX.MIS' STO     @ Max Number of Missles
  78.       10 'MNUM' STO        @ Missle Number of Current Shot
  79.       5 'TIME.MAG' STO     @ Time Magnification when off-screen
  80.       1E-3 'MIN.FORC' STO  @ Minumum Force to determine Missile lost
  81.       12 'VINIT' STO       @ Initial Missle Velocity
  82.       360 'Gk' STO         @ Gravitational Konstant
  83.       0 'ANGLE' STO        @ Initial Missle Velocity Angle
  84.       0.2 'DTIME' STO      @ Delta Time
  85.       { } 'POSITION' STO   @ Position/Mass Array, Station, Target, + Planets
  86.       MAX.PL 1 - RAND * IP 2 + 'NUM.P' STO  @ Random number of Planets (2-5)
  87.       GENSTAT
  88.       GENTARG
  89.       GENPLAN
  90.       ANGLE SETMIS @ Set missile data in case user doesn't first time
  91.     \>>
  92.   GETSEED  @ Set random number seed
  93.     \<< 
  94.       "Enter random seed or\010"
  95.       "Nothing if don't care\010" +
  96.       "Push ENTER" +
  97.       ""
  98.       INPUT DUP SIZE
  99.       IF 0 \=/
  100.       THEN
  101.         OBJ\-> RDZ @ If number entered, set seed
  102.       ELSE
  103.         DROP       @ Else clear stack
  104.       END
  105.     \>>
  106.   GENTARG  @ Locate and draw target in right half of screen
  107.     \<< 
  108.       POSITION
  109.       24 RAND * IP 87 +   @ X value
  110.       24 RAND * IP 20 +   @ Y value
  111.       T.SIZE              @ Mass (radius) value
  112.       3 \->LIST 1 \->LIST + 'POSITION' STO
  113.       DTARG
  114.     \>>
  115.   DTARG  @ Draw target
  116.     \<<
  117.       PICT POSITION 2 GET OBJ\-> DROP2 SWAP 4 - R\->B
  118.       SWAP 4 - R\->B 2 \->LIST TARG REPL
  119.     \>>
  120.   GENSTAT  @ Locate and draw station in left half of screen
  121.     \<<
  122.       POSITION
  123.       24 RAND * IP 20 +   @ X value
  124.       24 RAND * IP 20 +   @ Y value
  125.       S.SIZE              @ Mass (radius) value
  126.       3 \->LIST 1 \->LIST + 'POSITION' STO
  127.       DSTAT
  128.     \>>
  129.   DSTAT  @ Draw station
  130.     \<< 
  131.       PICT POSITION 1 GET OBJ\-> DROP2 SWAP 3 - R\->B
  132.       SWAP 3 - R\->B 2 \->LIST STAT REPL
  133.     \>>
  134.   GENPLAN  @ Locate and draw planets with separation
  135.     \<< 3 0 0 \-> i x y
  136.       \<<
  137.         DO
  138.           POSITION
  139.           110 RAND * IP 10 + DUP R\->B 'x' STO  @ X value
  140.           40 RAND * IP 10 + DUP R\->B 'y' STO   @ Y value
  141.           x y 2 \->LIST DUP 'x' STO PIXON       @ Show location temporarily
  142.           5 RAND * IP 2 +                       @ Mass value
  143.           3 \->LIST 1 \->LIST + 'POSITION' STO
  144.           1 CF
  145.           1 i 1 -
  146.           FOR j
  147.             i j CALDIFF
  148.             IF 12 <
  149.             THEN 1 SF
  150.             END
  151.           NEXT
  152.           x PIXOFF
  153.           IF 1 FC?
  154.           THEN      @ Location is valid, draw planet
  155.             i DPLAN 1 'i' STO+
  156.           ELSE      @ Invalid location, remove from list
  157.             POSITION DUP SIZE 1 - 1 SWAP SUB 'POSITION' STO
  158.           END
  159.         UNTIL i NUM.P 2 + >
  160.         END
  161.       \>>
  162.     \>>
  163.   DPLAN  @ Draw one planet
  164.     \<< 
  165.       POSITION SWAP GET OBJ\-> DROP R\->B 3 ROLLD
  166.       R\->B SWAP R\->B SWAP 2 \->LIST SWAP
  167.       0 6.28 RAD ARC
  168.     \>>
  169.   CALDIFF  @ Calculate distance between surface of planets i & j
  170.     \<< \-> i j
  171.       \<<  @ DIFF = SQRT((Yj-Yi)^2 + (Xj-Xi)^2) - Mj - Mi
  172.         POSITION j GET OBJ\-> DROP POSITION i GET OBJ\-> DROP
  173.         4 ROLL + 5 ROLLD SWAP 3 ROLLD - DUP * 3 ROLLD - DUP *
  174.         + \v/ SWAP -
  175.       \>>
  176.     \>>
  177.   REDRAW  @ Draw station, target, and planets again
  178.     \<< 
  179.       ERASE { #0d #0d } PVIEW DSTAT DTARG
  180.       3 NUM.P 2 +
  181.       FOR j
  182.         j DPLAN
  183.       NEXT
  184.     \>>
  185.   SENDMIS  @ Send a missile
  186.     \<<
  187.       3 CF            @ Outside Station Flag (clear for inside)
  188.       0 'HitSom' STO  @ 0 = hit nothing
  189.                       @ 1 = hit planet
  190.                       @ 2 = hit target
  191.                       @ 3 = hit station
  192.                       @ 4 = lost in space
  193.                       @ 5 = user aborted missile
  194.                       @ 6 = quit program
  195.       IF MNUM 0 ==
  196.       THEN 2 CF       @ Out missles, quit
  197.       ELSE
  198.         TEXT CLLCD    @ Activate and clear text screen
  199.         {
  200.           { "VIEW" \<< { #0d #0d} PVIEW WAITK DHELP \>> }
  201.           { "REDR" \<< REDRAW DHELP \>> }
  202.           { "SHOT" \<< TEXT CLLCD ""
  203.                      IF MNUM MAX.MIS \=/
  204.                      THEN
  205.                        "Last Angle was " +
  206.                        2 FIX ANGLE \->STR + "\010" +
  207.                      END
  208.                      "Input angle, push CONT" +
  209.                      "    90"   3 DISP
  210.                      "180 ** 0" 4 DISP
  211.                      "   -90"   5 DISP
  212.                      3 FREEZE
  213.                      PROMPT
  214.                      DUP 'ANGLE' STO SETMIS DHELP \>> }
  215.           { "CONT" CONT }
  216.           { "GO" CONT }
  217.           { "QUIT" \<< 2 CF 6 'HitSom' STO CONT \>> }
  218.         }
  219.         TMENU DHELP                  @ Display menu and help text
  220.         HALT
  221.         MNUM 1 - 'MNUM' STO          @ One less missile now
  222.         { #0d #0d } PVIEW
  223.         WHILE HitSom NOT             @ Until we hit something
  224.         REPEAT UPDATE DRAWXY EVALPOS @ Calculate missle movement
  225.         END
  226.       END
  227.     \>>
  228.   SETMIS  @ Set initial values for missile data, angle is on stack
  229.     \<<
  230.       NEG DUP DEG COS VINIT * 'VX' STO SIN 
  231.       VINIT * 'VY' STO POSITION 1 GET OBJ\-> DROP2 'YM' STO 
  232.       'XM' STO 
  233.     \>> 
  234.   DHELP  @ Display help for temporary menu
  235.     \<<
  236.       TEXT CLLCD 
  237.       "VIEW - See Planets"     1 DISP
  238.       "REDR - Redraw Screen"   2 DISP
  239.       "SHOT - Enter Angle"     3 DISP
  240.       "GO   - Launch Missile"  4 DISP
  241.       "QUIT - Restore and End" 5 DISP
  242.       MNUM STD \->STR " Missle" + 
  243.       IF MNUM 1 \=/ THEN "s" + END " Left" + 7 DISP
  244.       3 FREEZE
  245.     \>>
  246.   UPDATE  @ Update missile position and velocity
  247.     \<< 0 0 \-> fx fy
  248.       \<<
  249.         IF XM DUP 0 < SWAP 130 > OR
  250.            YM DUP 0 < SWAP  63 > OR OR  @ If off screen
  251.         THEN TIME.MAG DTIME *           @ increase time interval
  252.         ELSE DTIME 
  253.         END 'T' STO
  254.         3 NUM.P 2 +
  255.         FOR i                           @ For each planet
  256.           i CALDIST \-> x y m d         @ Cal dist to missle
  257.           \<<
  258.             y YM - m * Gk * d DUP DUP * * DUP 3 ROLLD /
  259.             'fy' STO+
  260.             x XM - m * Gk * SWAP /
  261.             'fx' STO+                   @ Sum resulting forces
  262.           \>>
  263.         NEXT
  264.         fx T * 'VX' STO+ fy T * 'VY' STO+  @ From total force
  265.         VX T * 'XM' STO+ VY T * 'YM' STO+  @ Find new velocity
  266.         fx DUP * fy DUP * + 'FORCE' STO    @ and missile location
  267.       \>>
  268.     \>>
  269.   CALDIST  @ Calculate distance from missile to center of planet
  270.     \<<
  271.       POSITION SWAP GET DUP OBJ\-> DROP2 YM - DUP * SWAP XM - DUP * + \v/
  272.       SWAP OBJ\-> DROP 4 ROLL   @ Return X, Y, Mass, Distance
  273.     \>>
  274.   DRAWXY  @ Draw pixel on screen for missile position
  275.     \<<
  276.       XM R\->B YM R\->B 2 \->LIST PIXON
  277.     \>>
  278.   EVALPOS  @ Evaluate missile position
  279.     \<<
  280.       IF KEY                      @ If a key is hit, abort missile
  281.       THEN
  282.         5 'HitSom' STO
  283.       ELSE
  284.         IF DTIME TIME.MAG * T ==  @ If off screen
  285.         THEN 
  286.           IF FORCE MIN.FORC <   @ Check if lost in space
  287.           THEN 4 'HitSom' STO 
  288.           END
  289.         ELSE                    @ Else when on screen
  290.           3 NUM.P 2 +           @ Check if we hit a planet
  291.           FOR i
  292.             i CALDIST \-> x y m d 
  293.             \<<
  294.               IF d m \<= THEN 1 'HitSom' STO END
  295.             \>>
  296.           NEXT
  297.           1 CALDIST             @ Check if we hit the station
  298.           IF 3 FC?              @ IF not outside station
  299.           THEN
  300.             IF S.SIZE 1 + >     @ Are we outside yet?
  301.             THEN 3 SF           @ We are now outside the station
  302.             END
  303.           ELSE                  @ We are outside
  304.             IF S.SIZE 1 + \<=   @ did we hit ourself?
  305.             THEN 3 'HitSom' STO
  306.             END
  307.           END
  308.           2 CALDIST             @ Check if we hit the target
  309.           IF T.SIZE 1 + \<= THEN 2 'HitSom' STO END
  310.         END
  311.       END
  312.       CLEAR                     @ Clear CALDIST stuff off stack
  313.       MESS                      @ Output appropriate message
  314.     \>>
  315.   MESS  @ Display appropriate message
  316.     \<<
  317.       IF HitSom
  318.       THEN
  319.         TEXT CLLCD              @ Display text screen and clear
  320.         CASE
  321.           HitSom 1 ==
  322.           THEN "Hit Planet"
  323.           END
  324.           HitSom 2 ==
  325.           THEN "You Hit the Target\010Using "
  326.             MAX.MIS MNUM - STD \->STR +
  327.             " Missile" +
  328.             IF MAX.MIS MNUM - 1 \=/
  329.             THEN "s" +
  330.             END
  331.             2 CF              @ Stop program
  332.           END
  333.           HitSom 3 ==
  334.           THEN 
  335.             "You Blew Yourself Up" 
  336.             2 CF              @ Stop program
  337.           END
  338.           HitSom 4 ==
  339.           THEN "Missle Escaped System"
  340.           END
  341.           HitSom 5 ==
  342.           THEN "Missle Aborted"
  343.           END
  344.         END
  345.         1 DISP 
  346.         IF MNUM 0 == 
  347.         THEN
  348.           "Game Over No Missiles" 3 DISP
  349.         END
  350.         "  Push any key EXCEPT"   4 DISP
  351.         "  ATTN/ON to continue"   5 DISP WAITK
  352.       END
  353.     \>>
  354.   WAITK  @ Wait for any key press
  355.     \<<
  356.       0 WAIT DROP
  357.     \>>
  358.   STAT GROB 7 7 C141F755F741C1                        @ Station cross
  359.   TARG GROB 9 9 8300EF006C00BB10BA10BB106C00EF008300  @ Target bullseye
  360. END
  361.